//
//  SecondViewController.m
//  Server
//
//  Created by 顾悦 on 16/3/21.
//
//

#import "MyOrdersViewController.h"
#import "CollectionViewCell.h"
#import "WaitReceiveViewController.h"
#import "OrderManagerService.h"
#import "UserInfoController.h"
#import "WaitServerViewController.h"
#import "NOPayViewController.h"
#import "AllOrderViewController.h"
#import "AfterSaleViewController.h"

@interface MyOrdersViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UICollectionView *collectionView;
@property(nonatomic,strong)NSArray * celltableArray;
@property(nonatomic,strong)NSArray * cellimageArray;

@property(nonatomic,strong)OrderManagerService * orderservice;
@property(nonatomic,copy)NSString * str1;
@property(nonatomic,copy)NSString * str2;
@property(nonatomic,copy)NSString * str3;
@property(nonatomic,copy)NSString * str4;
@property(nonatomic,copy)NSString * str5;

@end

@implementation MyOrdersViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = NO;
    [self.collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/3, [UIScreen mainScreen].bounds.size.width/3);
    //    layout.headerReferenceSize = CGSizeMake(100, 10);
    //    layout.footerReferenceSize = CGSizeMake(100, 20);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 250) collectionViewLayout:layout];
    //self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.bounces = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    
    //注册
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
    self.cellimageArray = @[@"待接单",@"待服务",@"未付款",@"全部订单",@"售后单"];
    self.celltableArray = @[@"指派单",@"待服务",@"未付款",@"全部订单",@"售后单"];
    //
    self.orderservice = [OrderManagerService new];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payFinished:) name:@"NotificationNamePayFinished" object:nil];

}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NotificationNamePayFinished" object:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self getOrderCountRequest];
}

//获取订单数量
-(void)getOrderCountRequest{

        [self.orderservice OrderCount:[UserInfoController userID] completionHanlder:^(NSArray *items, NSError *error) {
            self.str1 = [items valueForKey:@"WaitingOrderCount"];
            self.str2 = [items valueForKey:@"WaitingServiceOrderCount"];
            self.str3 = [items valueForKey:@"NoPayOrderCount"];
            self.str4 = [items valueForKey:@"AllOrderCount"];
            self.str5 = [items valueForKey:@"AfterSaleOrderCount"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        }];
    
}

#pragma mark -

- (void)payFinished:(NSNotification *)notification {
    ServerViewController *webViewController = ServerViewController.new;
    webViewController.hidesBottomBarWhenPushed = YES;
    NSNumber *userInfo = notification.object;
    if (!userInfo) {
        webViewController.HTMLURL = @"payment/finish.html";
    } else if ([userInfo respondsToSelector:@selector(boolValue)] && ![userInfo boolValue]) {
        webViewController.HTMLURL = @"payment/fail.html";
    }
    [self.navigationController pushViewController:webViewController animated:YES];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.imageview.image = [UIImage imageNamed:self.cellimageArray[indexPath.item]];
    cell.titleLB.text = self.celltableArray[indexPath.item];
    cell.countLB.textColor = [UIColor whiteColor];
    
    if (indexPath.item == 0) {
        if ([self.str1 integerValue] > 99) {
            cell.countLB.text = @"99+";
        }else{
            cell.countLB.text = self.str1;
        }
    }
    if (indexPath.item == 1) {
        if ([self.str2 integerValue] > 99) {
            cell.countLB.text = @"99+";
        }else{
            cell.countLB.text = self.str2;
        }
    }
    if (indexPath.item == 2) {
        if ([self.str3 integerValue] > 99) {
            cell.countLB.text = @"99+";
        }else{
            cell.countLB.text = self.str3;
        }
    }
    if (indexPath.item == 3) {
        if ([self.str4 integerValue] > 99) {
            cell.countLB.text = @"99+";
        }else{
            cell.countLB.text = self.str4;
        }
    }
    if (indexPath.item == 4) {
        if ([self.str5 integerValue] > 99) {
            cell.countLB.text = @"99+";
        }else{
            cell.countLB.text = self.str5;
        }
    }
    
    if ([cell.countLB.text integerValue] <= 0) {
        cell.countLB.hidden = YES;
    } else {
        cell.countLB.hidden = NO;
    }

    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.item) {
        case 0:
        {
            WaitReceiveViewController * vc = [[WaitReceiveViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            WaitServerViewController * vc = [[WaitServerViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            NOPayViewController * vc = [[NOPayViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            AllOrderViewController * vc = [[AllOrderViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            AfterSaleViewController * vc = [[AfterSaleViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor grayColor];
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
